#include "HeroCacheData.h"

HeroCacheData* HeroCacheData::mInstance = NULL;

void HeroCacheData::CreateInstance()
{
	if (mInstance == nullptr)
	{
		mInstance = new HeroCacheData();
	}
}

HeroCacheData* HeroCacheData::GetInstance()
{
	return mInstance;
}

HeroCacheData::HeroCacheData()
	: m_maxCount(0)
{
}

HeroCacheData::~HeroCacheData()
{
	Clear();
}

bool HeroCacheData::Reload()
{
	_safe_guard_begin;

	Clear();

	Initialization();

	_safe_guard_end;

	return false;
}

void HeroCacheData::Clear()
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.begin();
	for (; itor != m_mapCacheData.end(); ++itor)
	{
		_safe_delete(itor->second);
	}

	m_mapCacheData.clear();

	_safe_guard_end;
}

bool HeroCacheData::Initialization()
{
	_safe_guard_begin;

	Json::Value _jv = ReadJsonFile("./data/hero/hero.json");
	if (_jv == Json::nullValue)
	{
		LOG_ERROR("default","ReadJsonFile -> data/hero/hero.json failed ......");
		return false;
	}

	m_maxCount = _jv.size();

	Json::Value::Members _tmp = _jv.getMemberNames();
	uint32_t _size = (uint32_t)(_tmp.size());
	for (uint32_t i = 0; i < _size; ++i)
	{
		uint32_t _idx = (uint32_t)atoi(_tmp[i].c_str());

		AddData(_idx, _jv[_tmp[i]]);
	}

	return true;

	_safe_guard_end;

	return false;
}

void HeroCacheData::AddData(uint32_t id, Json::Value& jm)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor == m_mapCacheData.end())
	{
		Hero* p = new Hero;
		_safe_check(p);

		p->mId = jm["hero_id"].asUInt();
		p->mTypes = jm["types"].asUInt();
		strcpy(p->mName, jm["name"].asString().c_str());
		strcpy(p->mDescribe, jm["describe"].asString().c_str());
		strcpy(p->mInfo, jm["info"].asString().c_str());
		strcpy(p->mIconAtlas, jm["icon_atlas"].asString().c_str());
		strcpy(p->mIconName, jm["icon_name"].asString().c_str());
		strcpy(p->mModel, jm["model"].asString().c_str());
		strcpy(p->mOriginalPainting, jm["original_painting"].asString().c_str());
		p->mAttribute = jm["attribute"].asUInt();
		p->mReleased = jm["released"].asUInt();
		p->mIsIcon = jm["is_icon"].asUInt();
		p->mSex = jm["sex"].asUInt();
		p->mInitStar = jm["init_star"].asUInt();

		// [1.7,2.9,1.6]
		Json::Value rate1 = jm["rate1"];
		uint32_t sz = rate1.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mRate1[i] = rate1[i].asDouble();
		}

		// [1.7,2.9,1.6]
		Json::Value rate2 = jm["rate2"];
		sz = rate2.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mRate2[i] = rate2[i].asDouble();
		}

		// [1.7,2.9,1.6]
		Json::Value rate3 = jm["rate3"];
		sz = rate3.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mRate3[i] = rate3[i].asDouble();
		}

		// [1.7,2.9,1.6]
		Json::Value rate4 = jm["rate4"];
		sz = rate4.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mRate4[i] = rate4[i].asDouble();
		}

		// [1.7,2.9,1.6]
		Json::Value rate5 = jm["rate5"];
		sz = rate5.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mRate5[i] = rate5[i].asDouble();
		}

		p->mSoulGem = jm["soul_gem"].asUInt();

		Json::Value skillId = jm["skill_id"];
		sz = skillId.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mSkillId[i] = skillId[i].asUInt();
		}

		Json::Value passiveAttr = jm["passiveAttr"];
		sz = passiveAttr.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mPassiveAttr[i] = passiveAttr[i].asUInt();
		}

		Json::Value passiveAttr_unlockStar = jm["passiveAttr_unlockStar"];
		sz = passiveAttr_unlockStar.size();
		for (uint32_t i = 0; i < sz; ++i)
		{
			p->mPassiveAttr_unlockStar[i] = passiveAttr_unlockStar[i].asUInt();
		}

		m_mapCacheData[id] = p;
	}

	_safe_guard_end;
}

Hero* HeroCacheData::GetData(uint32_t id)
{
	_safe_guard_begin;

	auto itor = m_mapCacheData.find(id);
	if (itor != m_mapCacheData.end())
	{
		return itor->second;
	}
	_safe_guard_end;

	return nullptr;
}

bool HeroCacheData::GetDataJsonString(uint32_t id, Json::Value& _jm)
{
	_safe_guard_begin;

	Hero* p = GetData(id);
	if (nullptr == p)
	{
		LOG_ERROR("default","Error7");
		return false;
	}

	_jm.clear();

	_jm["id"] = Json::Value(p->mId);
	_jm["types"] = Json::Value(p->mTypes);
	_jm["name"] = Json::Value(p->mName);
	_jm["describe"] = Json::Value(p->mDescribe);
	_jm["info"] = Json::Value(p->mInfo);
	_jm["icon_atlas"] = Json::Value(p->mIconAtlas);
	_jm["icon_name"] = Json::Value(p->mIconName);
	_jm["model"] = Json::Value(p->mModel);
	_jm["original_painting"] = Json::Value(p->mOriginalPainting);
	_jm["attribute"] = Json::Value(p->mAttribute);
	_jm["released"] = Json::Value(p->mReleased);
	_jm["is_icon"] = Json::Value(p->mIsIcon);
	_jm["sex"] = Json::Value(p->mSex);
	_jm["init_star"] = Json::Value(p->mInitStar);

	Json::Value rate1;
	for (int i = 0; i < 3; ++i)
	{
		_safe_check(p->mRate1[i]);

		rate1.append(p->mRate1[i]);
	}
	_jm["rate1"] = rate1;

	Json::Value rate2;
	for (int i = 0; i < 3; ++i)
	{
		_safe_check(p->mRate2[i]);

		rate2.append(p->mRate2[i]);
	}
	_jm["rate2"] = rate2;

	Json::Value rate3;
	for (int i = 0; i < 3; ++i)
	{
		_safe_check(p->mRate3[i]);

		rate3.append(p->mRate3[i]);
	}
	_jm["rate3"] = rate3;

	Json::Value rate4;
	for (int i = 0; i < 3; ++i)
	{
		_safe_check(p->mRate4[i]);

		rate4.append(p->mRate4[i]);
	}
	_jm["rate4"] = rate4;

	Json::Value rate5;
	for (int i = 0; i < 3; ++i)
	{
		_safe_check(p->mRate5[i]);

		rate5.append(p->mRate5[i]);
	}
	_jm["rate5"] = rate5;

	_jm["soul_gem"] = Json::Value(p->mSoulGem);

	Json::Value skillId;
	for (int i = 0; i < 7; ++i)
	{
		_safe_check(p->mSkillId[i]);

		skillId.append(p->mSkillId[i]);
	}
	_jm["skill_id"] = skillId;

	Json::Value passiveAttr;
	for (int i = 0; i < 4; ++i)
	{
		_safe_check(p->mPassiveAttr[i]);

		passiveAttr.append(p->mPassiveAttr[i]);
	}
	_jm["passiveAttr"] = passiveAttr;

	Json::Value passiveAttr_unlockStar;
	for (int i = 0; i < 4; ++i)
	{
		_safe_check(p->mPassiveAttr_unlockStar[i]);

		passiveAttr_unlockStar.append(p->mPassiveAttr_unlockStar[i]);
	}
	_jm["passiveAttr_unlockStar"] = passiveAttr_unlockStar;

	_safe_guard_end;

	return true;
}

uint32_t HeroCacheData::GetDataCount()
{
	_safe_guard_begin;

	return m_maxCount;

	_safe_guard_end;
}